const express = require('express');
const router = express.Router();
const db = require('../../../../util/dbConfig/dbConfig')


router.post('/',async (req,res) => {
    let userInfo = req.session.userInfo
    let {
        eweId,      //母羊编号
        ramId,      //公羊编号
      sonId,      //羔羊编号
        beDate,   //配种时间
        sortLowNum,   //第几列的排序数，从左往右，从1开始
        ISAsc,         //增还是减  1为增，0为减
        page,      //页码
        isPage,     //是否分页 1为分页0为不分页
    }=req.body
    if(!page){
        return res.send({
            code: 1,
            msg: '请输入页码'
        })
    }
    let newPage = (page - 1) * 10 //跳过多页
    let arr = [
        eweId,      //母羊编号
        ramId,      //公羊编号
        sonId,      //羔羊编号
    ]
    let name = [
        'EWE_ID',
        'RAM_ID',
        'yang_id'
    ]
    let sex = []
    let sqlArr = []
    let sqlName = []

    //循环参数列表，将有值的参数添加到以上数组中，确定要添加的字段名
    for(let i = 0; i < arr.length; i++){
        if(arr[i] !== null && arr[i] !== ''){
            sqlArr.push(arr[i])
            sqlName.push(name[i])
        }
    }
    if(sqlName[0] === 'RAM_ID'){
        sex.push('1')
    }else if(sqlName[0] === 'EWE_ID'){
        sex.push('2')
    }else if(sqlName[0] === 'yang_id'){
    }else {
        return res.send({
            code: 4,
            msg: '服务异常，请稍后再试'
        })
    }
    console.log(sqlName);
    console.log(sqlArr);
    if(sqlArr.length !== 1 && sqlName.length !== 1){
        return res.send({
            code: 1,
            msg: '参数错误'
        })
    }
    try{
        let sql = `select
            a.ID, 
            c.NAME,
            a.EWE_ID,
            a.RAM_ID,
            a.br_date,
            a.cg_date,
            a.yang_id,
            a.sex,
            b.BIRTH_WEIGHT
            from hybridization a , sheep b , user c
            where a.${sqlName[0]}=b.ID and c.ID=b.USER_ID 
            `
        //如果是羔羊耳号查找
        if(sqlName[0] !== 'yang_id'){
            sql = sql + ' ' + `and b.SEX=${sex[0]}`
        }
        //如果用户为普通用户
        if(userInfo.admin === 0){
            sql = sql + ' ' + `and b.USER_ID = ${userInfo._id}`
        }
        //sql语句拼接，为了条件查询
        sql = sql + ` and a.${sqlName[0]} = ${sqlArr[0]}`
        //总结果数sql语句
        let countSql = `SELECT count(*) FROM  (${sql}) as d`
        //排序查询
        if(sortLowNum !== null && sortLowNum !== '' && ISAsc !== null && ISAsc !== ''){
          if(ISAsc === 1){
              //升序
              sql = sql + ' ' + `order by ${sortLowNum} asc`
          }else if(ISAsc === 0){
              //降序
              sql = sql + ' ' +`order by ${sortLowNum} desc`
          }
      }
        //分页sql
        if(isPage === 1){
            sql = sql + ' ' +`limit ${newPage},10`
        }
        let result = await db.SySqlConnect(sql,sqlArr)
        if(result.length === 0 || !result){
            return res.send({
                code: 2,
                msg: '您没有羊'
            })
        }
        let countAll = await db.SySqlConnect(countSql,sqlArr)
        countAll = countAll[0]['count(*)']
        res.send({
            code: 0,
            msg: `查询成功${countAll}条`,
            data: {
                userInfo,  //用户信息
                countAll,  //总结果数
                length: result.length, //当页结果数
                result,    //用户羊只羊绒信息
            }
        })
    }catch (e) {
        console.log(e);
        return res.send({
            code: 4,
            msg: '服务器错误，请稍后再试'
        })
    }
})

module.exports = router
